package util;

import ivan.bookKeeper.R;

import java.util.HashMap;
import java.util.Map;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class DialogCreator {
	private Map<String, Object> map = new HashMap<String, Object>();
	private String enteredText = "";
	private String TAG;
	private BaseExpandableListAdapter expListAdapter;
	
	public DialogCreator(Map<String, Object> map) {
		super();
		this.map = map;
	}

	public DialogCreator(Map<String, Object> map, String TAG,
			BaseExpandableListAdapter expListAdapter) {
		super();
		this.map = map;	
		this.TAG = TAG;
		Log.d(TAG, "Map given to DialogCreator: " + this.map.size());
		this.expListAdapter = expListAdapter;
	}

	public Map<String, Object> getMap() {
		return map;
	}

	public BaseExpandableListAdapter getExpListAdapter() {
		return expListAdapter;
	}

	public void setExpListAdapter(BaseExpandableListAdapter expListAdapter) {
		this.expListAdapter = expListAdapter;
	}

	// DIALOG FOR CREATING INPUTS
	public Dialog createDialog(final Context context, String title,
			final String text, final View viewToUpdate, final boolean isNumber) {
		LayoutInflater inflater = LayoutInflater.from(context);
		final View textEntryView = inflater.inflate(
				R.layout.alert_dialog_text_entry, null);
		return new AlertDialog.Builder(context)
				.setIcon(android.R.drawable.ic_input_add)
				.setTitle(title)
				.setView(textEntryView)
				.setPositiveButton(text, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int whichButton) {

						EditText et = (EditText) textEntryView
								.findViewById(R.id.enterData);
						enteredText = et.getText().toString();
						Log.d(TAG, "Entered: " + enteredText);
						// check if isNumber
						if (isNumber) {
							try {
								Integer.parseInt(enteredText);
								TextView tv = (TextView) viewToUpdate;
								tv.setText(enteredText);
								Log.d(TAG, "UPDATED TEXT: " + enteredText);
								// puts the value in map; can be retrieved by
								// the
								// database cursor
								map.put(text, enteredText);
								// notifies list of the changes (in case the
								// dialog
								// is from list textView)
								expListAdapter.notifyDataSetChanged();
							} catch (NumberFormatException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
								et.setText("");
								Toast.makeText(
										context,
										context.getString(R.string.error_parse_number),
										Toast.LENGTH_LONG).show();
							}
						} else {
							TextView tv = (TextView) viewToUpdate;
							tv.setText(enteredText);
							Log.d(TAG, "UPDATED TEXT: " + enteredText);
							// puts the value in map; can be retrieved by the
							// database cursor
							map.put(text, enteredText);
							// notifies list of the changes (in case the
							// dialog
							// is from list textView)
							expListAdapter.notifyDataSetChanged();
						}
					}
				})
				.setNegativeButton(R.string.txt_cancel,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.cancel();
								Log.d(TAG, "User Canceled!!!");
							}
						}).create();

	}
}
